const {login}=require('../controller/user')
const {SuccessModel,ErrorModel}=require('../model/resModel')
const getCookieExpires=()=>{
    const d=new Date()
    d.setTime(d.getTime()+(24*60*60*1000))
    return d.toGMTString()
    //cooke过期时间

}

const handelUserRouter=(req,res)=>{
    const method=req.method
    

    if(method==='POST'&&req.path==='/api/user/login'){
        const {username,password}=req.body
        const result=login(username,password)
        return result.then(data=>{
            if(data.username){
                return new  SuccessModel('登陆成功')
    
            }
            else{
                return new ErrorModel('登陆失败')
            }

        })
        

       
    }
    if(method==="GET" && req.path==="/api/user/login-test"){
        if(req.cookie.username){
            return   Promise.resolve(new SuccessModel())  
        }
        else{
            return Promise.resolve(new ErrorModel("未登录"))  
        }
    }
}
module.exports=handelUserRouter